package org.rpgmapper.view;

import org.rpgmapper.R;
import org.rpgmapper.adapter.CharacterAdapter;
import org.rpgmapper.adapter.MapAdapter;
import org.rpgmapper.bo.Character;
import org.rpgmapper.database.RPGMapperOpenHelper;

import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
import org.rpgmapper.bo.Stat;


public class ConfigureSkirmishView extends FrameLayout {

	private boolean isVisible = false;
	private int SELECTED_TAB = Color.WHITE;//Color.argb(0xFF, 0xdf, 0xe9, 0xf0);
	private int UNSELECTED_TAB = Color.argb(0xBB, 0xFF, 0xFF, 0xFF);
	private ViewGroup vg;
	private Character selectedCharacter;
	
	public ConfigureSkirmishView(Context context) {
		super(context);
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.configure_skirmish, this);
		init();
	}

	public ConfigureSkirmishView(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.configure_skirmish, this);
		init();
	}
	
	private void init() {
		//((ImageView)findViewById(R.id.tab_icon_map)).getDrawable().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
		((ImageView)findViewById(R.id.tab_icon_map)).setBackgroundColor(SELECTED_TAB);
		((ImageView)findViewById(R.id.tab_icon_map)).setOnClickListener(mapClick);
		((ImageView)findViewById(R.id.tab_icon_characters)).setOnClickListener(heroesClick);
		((ImageView)findViewById(R.id.tab_icon_monsters)).setOnClickListener(monstersClick);
		
		/*findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ConfigureSkirmishView.this.hide();
			}
		});*/

		findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ConfigureSkirmishView.this.hide();
			}
		});
		
		initGridViews();
	}
	
	public void selectTabAtIndex(int index) {
		switch (index) {
		case 0:
			mapClick.onClick(((ImageView)findViewById(R.id.tab_icon_map)));
			break;
		case 1:
			heroesClick.onClick(((ImageView)findViewById(R.id.tab_icon_characters)));
			break;
		case 2:
			monstersClick.onClick(((ImageView)findViewById(R.id.tab_icon_monsters)));
			break;
		default:
			break;
		}
	}
	
	public void toggle() {
		if (isVisible) {
			this.setVisibility(View.GONE);
			/*ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 0f);
			alpha.setDuration(100);
			alpha.start();*/
			isVisible = false;
		} else {
			this.setVisibility(View.VISIBLE);
			/*ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 1f);
			alpha.setDuration(100);
			alpha.start();*/
			this.selectTabAtIndex(0);
			isVisible = true;
		}
	}
	
	public void hide() {
		if (isVisible) {
			this.setVisibility(View.GONE);
			/*ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 0f);
			alpha.setDuration(100);
			alpha.start();*/
			isVisible = false;
		}
	}
	
	public void show() {
		if (! isVisible) {
			this.setVisibility(View.VISIBLE);
			/*ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 1f);
			alpha.setDuration(100);
			alpha.start();*/
			this.selectTabAtIndex(0);
			isVisible = true;
		}
	}
	
	private void initGridViews() {
        ((GridView)findViewById(R.id.maps)).setAdapter(new MapAdapter(this.getContext()));
        ((GridView)findViewById(R.id.PCs)).setAdapter(new CharacterAdapter(this.getContext(), false));
        ((GridView)findViewById(R.id.NPCs)).setAdapter(new CharacterAdapter(this.getContext(), true));

        ((GridView)findViewById(R.id.maps)).setOnItemClickListener(onMapItemClickListener);
        ((GridView)findViewById(R.id.PCs)).setOnItemClickListener(onPCItemClickListener);
        ((GridView)findViewById(R.id.PCs)).setOnItemLongClickListener(onItemLongClickListener);
        ((GridView)findViewById(R.id.NPCs)).setOnItemClickListener(onNPCItemClickListener);
        ((GridView)findViewById(R.id.NPCs)).setOnItemLongClickListener(onItemLongClickListener);
	}
	
	private OnItemClickListener onMapItemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> adapterView, View v, int arg2, long arg3) {
			ImageView imageView = (ImageView)v;
			if (adapterView.getTag() != null) {
				((ImageView)adapterView.getTag()).setBackgroundColor(Color.TRANSPARENT);
			}
			imageView.setBackgroundColor(Color.rgb(0x48, 0xB0, 0xF2));
			adapterView.setTag(imageView);
		}
	};

	private OnItemClickListener onPCItemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> adapterView, View v, int arg2, long arg3) {
			selectedCharacter = (Character)v.getTag();
			ImageView imageView = (ImageView)v.findViewById(R.id.avatar);
			if (adapterView.getTag() != null) {
				((ImageView)adapterView.getTag()).setBackgroundColor(Color.TRANSPARENT);
			}
			vg = (ViewGroup)findViewById(R.id.pc_panel);
			vg.setVisibility(View.VISIBLE);
			imageView.setBackgroundColor(Color.rgb(0x48, 0xB0, 0xF2));
			adapterView.setTag(imageView);

			((SeekBar)vg.findViewById(R.id.seekBarHitPoints)).setMax(selectedCharacter.get(Stat.MaxHitPoints));
			ObjectAnimator hitPoints = ObjectAnimator.ofInt(((SeekBar)vg.findViewById(R.id.seekBarHitPoints)), "progress", selectedCharacter.get(Stat.HitPoints));
			hitPoints.setDuration(200);
			hitPoints.start();
			//((SeekBar)vg.findViewById(R.id.seekBarHitPoints)).setProgress(selectedCharacter.getHitPoints());
			//((TextView)vg.findViewById(R.id.hitPoints)).setText(selectedCharacter.getHitPoints()+" / " + selectedCharacter.getMaxHitPoints());
			((SeekBar)vg.findViewById(R.id.seekBarHitPoints)).setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onStopTrackingTouch(SeekBar seekBar) {
					selectedCharacter.setBase(Stat.HitPoints,seekBar.getProgress());
					RPGMapperOpenHelper helper = new RPGMapperOpenHelper(getContext());
					helper.updateCharacter(selectedCharacter);
				}
				public void onStartTrackingTouch(SeekBar seekBar) {
				}
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					((TextView)vg.findViewById(R.id.hitPoints)).setText(progress+" / " + seekBar.getMax());
				}
			});

			ObjectAnimator init = ObjectAnimator.ofInt(((SeekBar)vg.findViewById(R.id.seekBarInitiative)), "progress", selectedCharacter.get(Stat.Init));
			init.setDuration(200);
			init.start();
			//((SeekBar)vg.findViewById(R.id.seekBarInitiative)).setProgress(selectedCharacter.getInitiative());
			//((TextView)vg.findViewById(R.id.initiative)).setText(""+selectedCharacter.getInitiative());
			((SeekBar)vg.findViewById(R.id.seekBarInitiative)).setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onStopTrackingTouch(SeekBar seekBar) {
					selectedCharacter.setBase(Stat.Init,seekBar.getProgress());
					RPGMapperOpenHelper helper = new RPGMapperOpenHelper(getContext());
					helper.updateCharacter(selectedCharacter);
				}
				public void onStartTrackingTouch(SeekBar seekBar) {
				}
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					((TextView)vg.findViewById(R.id.initiative)).setText(""+progress);
				}
			});
		}
	};

	private OnItemClickListener onNPCItemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> adapterView, View v, int arg2, long arg3) {
			selectedCharacter = (Character)v.getTag();
			ImageView imageView = (ImageView)v.findViewById(R.id.avatar);
			if (adapterView.getTag() != null) {
				((ImageView)adapterView.getTag()).setBackgroundColor(Color.TRANSPARENT);
			}
			vg = (ViewGroup)findViewById(R.id.npc_panel);
			vg.setVisibility(View.VISIBLE);
			imageView.setBackgroundColor(Color.rgb(0x48, 0xB0, 0xF2));
			adapterView.setTag(imageView);

			ObjectAnimator init = ObjectAnimator.ofInt(((SeekBar)vg.findViewById(R.id.seekBarInitiative)), "progress", selectedCharacter.get(Stat.Init));
			init.setDuration(200);
			init.start();
			//((SeekBar)vg.findViewById(R.id.seekBarInitiative)).setProgress(selectedCharacter.getInitiative());
			//((TextView)vg.findViewById(R.id.initiative)).setText(""+selectedCharacter.getInitiative());
			((SeekBar)vg.findViewById(R.id.seekBarInitiative)).setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onStopTrackingTouch(SeekBar seekBar) {
					selectedCharacter.setBase(Stat.Init,seekBar.getProgress());
					RPGMapperOpenHelper helper = new RPGMapperOpenHelper(getContext());
					helper.updateCharacter(selectedCharacter);
				}
				public void onStartTrackingTouch(SeekBar seekBar) {
				}
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					((TextView)vg.findViewById(R.id.initiative)).setText(""+progress);
				}
			});
			
			findViewById(R.id.delete_monster).setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
	                if (selectedCharacter != null) {
	                	AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
	                	builder.setMessage("Are you sure you want to remove this character ?")
	                	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	                	           public void onClick(DialogInterface dialog, int id) {
	        		                	RPGMapperOpenHelper helper = new RPGMapperOpenHelper(getContext());
	        			                helper.deleteCharacter(selectedCharacter.getId());
	        							invalidateMonsters();
	        							selectedCharacter = null;
	                	           }
	                	       })
	                	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
	                	           public void onClick(DialogInterface dialog, int id) {
	                	                dialog.cancel();
	                	           }
	                	       });
	                	builder.create().show();
	                }
				}
			});
		}
	};

	private OnItemLongClickListener onItemLongClickListener = new OnItemLongClickListener() {
		public boolean onItemLongClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
			final CharSequence text = ""+((Character)v.getTag()).getId();                        
		    ClipData.Item item = new ClipData.Item(text);
		    ClipData dragData = new ClipData(text, new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}, item);
		    DragShadowBuilder myShadow = new DragShadowBuilder(v);
            v.startDrag(dragData, myShadow, null, 0);
            
            return true;
		}
	};
	
	private OnClickListener monstersClick = new View.OnClickListener() {
		public void onClick(View v) {
	        ((GridView)findViewById(R.id.NPCs)).setAdapter(new CharacterAdapter(getContext(), true));
	        
			((ImageView)findViewById(R.id.tab_icon_map)).setBackgroundColor(UNSELECTED_TAB);
			((ImageView)findViewById(R.id.tab_icon_characters)).setBackgroundColor(UNSELECTED_TAB);
			((ImageView)findViewById(R.id.tab_icon_monsters)).setBackgroundColor(SELECTED_TAB);
			findViewById(R.id.tab1).setVisibility(View.GONE);
			findViewById(R.id.tab2).setVisibility(View.GONE);
			findViewById(R.id.tab3).setVisibility(View.VISIBLE);
			LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)findViewById(R.id.tab_icon_map).getLayoutParams();
	        lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, 2);
	        lp = (LinearLayout.LayoutParams)findViewById(R.id.tab_icon_characters).getLayoutParams();
	        lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, 2);
	        lp = (LinearLayout.LayoutParams)findViewById(R.id.tab_icon_monsters).getLayoutParams();
	        lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, 0);
		}
	};
	
	private OnClickListener heroesClick = new View.OnClickListener() {
		public void onClick(View v) {
	        ((GridView)findViewById(R.id.PCs)).setAdapter(new CharacterAdapter(getContext(), false));
			((ImageView)findViewById(R.id.tab_icon_map)).setBackgroundColor(UNSELECTED_TAB);
			((ImageView)findViewById(R.id.tab_icon_characters)).setBackgroundColor(SELECTED_TAB);
			((ImageView)findViewById(R.id.tab_icon_monsters)).setBackgroundColor(UNSELECTED_TAB);
			findViewById(R.id.tab1).setVisibility(View.GONE);
			findViewById(R.id.tab2).setVisibility(View.VISIBLE);
			findViewById(R.id.tab3).setVisibility(View.GONE);
			LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)findViewById(R.id.tab_icon_map).getLayoutParams();
	        lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, 2);
	        lp = (LinearLayout.LayoutParams)findViewById(R.id.tab_icon_characters).getLayoutParams();
	        lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, 0);
	        lp = (LinearLayout.LayoutParams)findViewById(R.id.tab_icon_monsters).getLayoutParams();
	        lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, 2);
		}
	};
	
	private OnClickListener mapClick = new View.OnClickListener() {
		public void onClick(View v) {
	        ((GridView)findViewById(R.id.maps)).setAdapter(new MapAdapter(getContext()));
			((ImageView)findViewById(R.id.tab_icon_map)).setBackgroundColor(SELECTED_TAB);
			((ImageView)findViewById(R.id.tab_icon_characters)).setBackgroundColor(UNSELECTED_TAB);
			((ImageView)findViewById(R.id.tab_icon_monsters)).setBackgroundColor(UNSELECTED_TAB);
			findViewById(R.id.tab1).setVisibility(View.VISIBLE);
			findViewById(R.id.tab2).setVisibility(View.GONE);
			findViewById(R.id.tab3).setVisibility(View.GONE);
			LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)findViewById(R.id.tab_icon_map).getLayoutParams();
	        lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, 0);
	        lp = (LinearLayout.LayoutParams)findViewById(R.id.tab_icon_characters).getLayoutParams();
	        lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, 2);
	        lp = (LinearLayout.LayoutParams)findViewById(R.id.tab_icon_monsters).getLayoutParams();
	        lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, 2);
		}
	};
	
	private void invalidateMonsters() {
		((GridView)findViewById(R.id.NPCs)).setAdapter(new CharacterAdapter(getContext(), true));
	}
	
}
